﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.DKChung
{
    public partial class DkChung_KhoNoiBo : UserControl
    {
        public DkChung_KhoNoiBo()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            gridKho.DataSource = DataAll.SelectAll("Kho_KhoHang", "*", "");
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvKho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvKho.OptionsView.ShowGroupPanel = false;
            gvKho.OptionsView.ColumnAutoWidth = true;
            gvKho.OptionsFind.AlwaysVisible = true;
            gvKho.Columns["IDKhoHang"].Visible = false;
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            var dkkho = new DkChung_KhoNoiBo_ThemMoi();
            dkkho.ShowDialog();
            LoadData();
        }

        private void gvKho_DoubleClick(object sender, EventArgs e)
        {
            if (gvKho.FocusedRowHandle >= 0)
            {
                DkChung_KhoNoiBo_ThemMoi.MaID = gvKho.Columns["IDKhoHang"].View.GetRowCellDisplayText(gvKho.FocusedRowHandle, "IDKhoHang");
                var dkkho = new DkChung_KhoNoiBo_ThemMoi();
                dkkho.ShowDialog();
                LoadData();
                DkChung_KhoNoiBo_ThemMoi.MaID = "0";
            }
        }

        private void btnXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvKho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa kho này ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Delete("Kho_KhoHang", "IDKhoHang = " + dataview[gvKho.FocusedRowHandle]["IDKhoHang"]);
                if (message.Equals("success"))
                {
                    DataAll.Delete("Kho_KhoHang_VatTu", "IDKhoHang = " + dataview[gvKho.FocusedRowHandle]["IDKhoHang"]);
                }
                MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            LoadData();
        }

    }
}
